package com.bluestone.scripts;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

import com.bluestone.util.Util;

public class ModuleTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		generateAIVRModule();

	}
	
	public static void generateAIVRModule(){
		try {
			Module module = new Module();
			module.setName("AIVR_Callflow");
            
			Page welcomePage = new Page();
			welcomePage.setName("welcomePage");
			Element welcomeElement = new Element();
			welcomeElement.setType("text");
			welcomeElement.setName("welcometext");
			welcomePage.addElement(welcomeElement);			
			module.addPage(welcomePage);
			
			Page selectlanguagePage = new Page();
			selectlanguagePage.setName("selectlanguagePage");
			Element selectlanguageElement = new Element();
			selectlanguageElement.setType("menu");
			selectlanguageElement.setName("selectlanguagemenu");
			selectlanguagePage.addElement(selectlanguageElement);
			module.addPage(selectlanguagePage);
			
			Page selectloginPage = new Page();
			selectloginPage.setName("selectloginPage");
			Element selectloginElement = new Element();
			selectloginElement.setType("menu");
			selectloginElement.setName("selectloginmenu");
			selectloginPage.addElement(selectloginElement);
			module.addPage(selectloginPage);
			
			Page userloginPage = new Page();
			userloginPage.setName("userloginPage");
			Element usernameElement = new Element();
			usernameElement.setType("textfield");
			usernameElement.setName("usernametextfield");
			userloginPage.addElement(usernameElement);
			Element passwordElement = new Element();
			passwordElement.setType("textfield");
			passwordElement.setName("passwordtextfield");
			userloginPage.addElement(passwordElement);
			module.addPage(userloginPage);
			
			Page guestloginPage = new Page();
			guestloginPage.setName("guestloginPage");
			module.addPage(guestloginPage);
			
			Page mainmenuPage = new Page();
			mainmenuPage.setName("mainmenuPage");
			Element mainmenuElement = new Element();
			mainmenuElement.setType("menu");
			mainmenuElement.setName("mainmenu");
			mainmenuPage.addElement(mainmenuElement);
			module.addPage(mainmenuPage);
			
			Page usersettingPage = new Page();
			usersettingPage.setName("usersettingPage");
			Element usersettingElement = new Element();
			usersettingElement.setType("menu");
			usersettingElement.setName("usersettingmenu");
			usersettingPage.addElement(usersettingElement);
			module.addPage(usersettingPage);
			
			Page getuserstatusPage = new Page();
			getuserstatusPage.setName("getuserstatusPage");
			module.addPage(getuserstatusPage);
			
			Page changefaxPage = new Page();
			changefaxPage.setName("changefaxPage");
			module.addPage(changefaxPage);
			
			Page changephonePage = new Page();
			changephonePage.setName("changephonePage");
			module.addPage(changephonePage);
			
			Page changepinnumberPage = new Page();
			changepinnumberPage.setName("changepinnumberPage");
			module.addPage(changepinnumberPage);
			
			JAXBContext context = JAXBContext.newInstance(module.getClass());
			Marshaller marshaller = context.createMarshaller();
			marshaller.setProperty("jaxb.formatted.output",new Boolean(true));
			
			// create parent folder if it doesn't exist to avoid IOException
			String path =  "./scripts/aivr_callflow.xml";
			File file = new File(path);
			if ( file.getParentFile()!=null && !file.getParentFile().exists() ) {
				file.getParentFile().mkdirs();
			}	
			marshaller.marshal(module, file);
			
		}catch(Exception me) {
			Util.getLogger().error("fail to save module "+me);
		}			
	}
	public static void generateModule(){
		try {
			Module module = new Module();
			module.setName("test");

			
			JAXBContext context = JAXBContext.newInstance(module.getClass());
			Marshaller marshaller = context.createMarshaller();
			marshaller.setProperty("jaxb.formatted.output",new Boolean(true));
			
			// create parent folder if it doesn't exist to avoid IOException
			String path =  "./scripts/module.xml";
			File file = new File(path);
			if ( file.getParentFile()!=null && !file.getParentFile().exists() ) {
				file.getParentFile().mkdirs();
			}	
			marshaller.marshal(module, file);
			
		}catch(Exception me) {
			Util.getLogger().error("fail to save module "+me);
		}			
	}
}
